package com.yeyks.app.controller.base.consumer;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yeyks.app.annotation.LoadUserId;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.param.consumer.UserIdParam;
import com.yeyks.consumer.dal.domain.UserAddress;
import com.yeyks.consumer.param.UserAddressUpdateParam;
import com.yeyks.consumer.service.UserAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yun
 * @since 2020-04-15
 */
@Api(tags = "用户地址", description = "用户地址")
@Slf4j
@RestController
@RequestMapping("/app/user/address")
public class UserAddressController {

    @Autowired
    private UserAddressService userAddressService;

    @ApiOperation(value = "获取地址:v4.4.3")
    @PostMapping(value = "/get")
    public AjaxResponseBody<UserAddress> get(@LoadUserId UserIdParam param) {
        return AjaxResponseBody.success(userAddressService.getOne(
                new QueryWrapper<UserAddress>()
                        .eq(UserAddress.USER_ID, param.getUserId())
                        .last("limit 1")
        ));
    }

    @ApiOperation(value = "添加或修改地址:v4.4.3")
    @PostMapping(value = "/update")
    public AjaxResponseBody<Boolean> update(@LoadUserId UserAddressUpdateParam param) {
        UserAddress userAddress = BeanUtil.toBean(param, UserAddress.class);
        if (Objects.isNull(userAddress.getId())) {
            userAddressService.save(userAddress);
        } else {
            userAddressService.updateById(userAddress);
        }
        return AjaxResponseBody.success(true);
    }

}
